package de.bazinga.rotw.menu;

import java.util.Iterator;

import com.jme.image.Texture;
import com.jme.math.Vector3f;
import com.jme.renderer.ColorRGBA;
import com.jme.renderer.Renderer;
import com.jme.scene.Spatial;
import com.jme.scene.shape.Quad;
import com.jme.scene.state.TextureState;
import com.jme.util.TextureManager;
import com.jmex.game.state.GameStateManager;

import de.bazinga.rotw.game.Game;
import de.bazinga.rotw.game.RotwGameState;
import de.bazinga.rotw.menu.item.MenuItem;

public class GameMenu extends Menu {

	public GameMenu() {
		super("gameMenu");
	}
	
	
	protected void init() {		
		// Background
		Quad bgQuad = new Quad("bg",
				Game.getGame().getDisplay().getWidth(), 
				Game.getGame().getDisplay().getHeight());
		
		bgQuad.setRenderQueueMode(Renderer.QUEUE_ORTHO);   
	 	
		bgQuad.setLocalTranslation(new Vector3f(
				Game.getGame().getDisplay().getWidth()/2,
				Game.getGame().getDisplay().getHeight()/2,
				0));
		
		TextureState bg = Game.getGame().getDisplay().getRenderer().createTextureState();
		bg.setEnabled(true);
		bg.setTexture(TextureManager.loadTexture(RotwGameState.class
				.getClassLoader().getResource("background/mainMenu.png"),
				Texture.MinificationFilter.BilinearNearestMipMap,
				Texture.MagnificationFilter.Bilinear));
		
		bgQuad.setRenderState(bg);
		
		bgQuad.setLightCombineMode(Spatial.LightCombineMode.Off);
		bgQuad.updateRenderState();
		
		getRootNode().attachChild(bgQuad);
	}
	
	
	protected void initMenuItems() {		
		menuItems.put("newServer", 
				new MenuItem("network/newServer.png",
						"newServer",
						1f,
						Game.getGame().getDisplay().getWidth() / 2.0f,
						Game.getGame().getDisplay().getHeight() - 150,
						250,100));

		menuItems.put("lan", 
				new MenuItem("network/lan.png",
						"lan",
						1f,
						Game.getGame().getDisplay().getWidth() / 2.0f,
						Game.getGame().getDisplay().getHeight() - 250,
						250,100));

		menuItems.put("internet", 
				new MenuItem("network/internet.png",
						"internet",
						1f,
						Game.getGame().getDisplay().getWidth() / 2.0f,
						Game.getGame().getDisplay().getHeight() - 350,
						250,100));

		menuItems.put("back", 
				new MenuItem("back.png",
						"back",
						1f,
						Game.getGame().getDisplay().getWidth() / 2.0f,
						Game.getGame().getDisplay().getHeight() - 450,
						250,100));
		
	}
	
	protected void handleClick(String item) {
		if(menuItems.containsKey(item)) {
			if(item.equals("newServer")) {
				GameStateManager.getInstance().getChild("serverMenu").setActive(true);
				this.setActive(false);
			}else if(item.equals("lan")) {
				GameStateManager.getInstance().getChild("lanMenu").setActive(true);
				this.setActive(false);
			}else if(item.equals("internet")) {
				GameStateManager.getInstance().getChild("internetMenu").setActive(true);
				this.setActive(false);
			}else if(item.equals("back")) {
				GameStateManager.getInstance().getChild("mainMenu").setActive(true);
				this.setActive(false);
			}
		}
	}
	
	protected void handleHover(MenuItem hoverItem) {
		Iterator<MenuItem> it = menuItems.values().iterator();
		while(it.hasNext()) {
			MenuItem item = it.next();
			if(hoverItem == item) {
				if(item  == menuItems.get("newServer")) {
					item.setText("network/newServerHover.png");
				}else if(item  == menuItems.get("lan")) {
					item.setText("network/lanHover.png");
				}else if(item  == menuItems.get("internet")) {
					item.setText("network/internetHover.png");
				}else if(item  == menuItems.get("back")) {
					item.setText("backHover.png");
				}else{
					item.setTextColor(ColorRGBA.yellow);
				}
			}else{
				if(item  == menuItems.get("newServer")) {
					item.setText("network/newServer.png");
				}else if(item  == menuItems.get("lan")) {
					item.setText("network/lan.png");
				}else if(item  == menuItems.get("internet")) {
					item.setText("network/internet.png");
				}else if(item  == menuItems.get("back")) {
					item.setText("back.png");
				}else{
					item.setTextColor(ColorRGBA.black);
				}
			}
		}
	}
}